//
//  OrderViewController.m
//  ifengNewsOrderDemo
//
//  Created by zer0 on 14-2-27.
//  Copyright (c) 2014年 zer0. All rights reserved.
//

#import "OrderViewController.h"
#import "TouchViewModel.h"
#import "TouchView.h"
#import "Header.h"

@interface OrderViewController ()

@end

@implementation OrderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.navigationItem.title = @"栏目管理";
    
    UIButton *leftBtn = [[UIButton alloc] init];
    leftBtn.frame = CGRectMake(0, 0, 14, 20);
    [leftBtn setImage:[UIImage imageNamed:@"btn_back_normal"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    NSString * string = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * filePath = [string stringByAppendingString:@"/modelArray0.swh"];
    NSString * filePath1 = [string stringByAppendingString:@"/modelArray1.swh"];
    
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray * channelListArr = self.titleArr;
        NSArray * channelUrlStringListArr = self.urlStringArr;
        NSMutableArray * mutArr = [NSMutableArray array];
        for (int i = 0; i < [channelListArr count]; i++) {
            NSString * title = [channelListArr objectAtIndex:i];
            NSString * urlString = [channelUrlStringListArr objectAtIndex:i];
            TouchViewModel * touchViewModel = [[TouchViewModel alloc] initWithTitle:title urlString:urlString];
            [mutArr addObject:touchViewModel];
            [touchViewModel release];
            if (i == KDefaultCountOfUpsideList - 1) {
                NSData * data = [NSKeyedArchiver archivedDataWithRootObject:mutArr];
                [data writeToFile:filePath atomically:YES];
                [mutArr removeAllObjects];
            }
            else if(i == [channelListArr count] - 1){
                NSData * data = [NSKeyedArchiver archivedDataWithRootObject:mutArr];
                [data writeToFile:filePath1 atomically:YES];
            }
            
        }
    }
    
    _modelArr1 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSArray * modelArr2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath1];
    _viewArr1 = [[NSMutableArray alloc] init];
    _viewArr2 = [[NSMutableArray alloc] init];
    
    
    
    //这里修改标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 66, ScreenWidth-20, 40)];
    
    _titleLabel.text = @"我的频道";
    _titleLabel.textColor = SYGoodColor;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:_titleLabel];
    
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 200, 38)];
    tipLabel.font = [UIFont systemFontOfSize:12];
    tipLabel.text = @"单击增删频道，拖拽排序";
    tipLabel.textColor = SYGoodColor;
    [_titleLabel addSubview:tipLabel];
    
    //修改二标题
    _titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, KTableStartPointY + KButtonHeight * ([self array2StartY] - 1) + KMoreChannelDeltaHeight, ScreenWidth-20, 40)];
    _titleLabel2.text = @"更多频道";
     _titleLabel2.textColor = SYGoodColor;
     _titleLabel2.textAlignment = NSTextAlignmentLeft;
    _titleLabel2.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:_titleLabel2];
    
    for (int i = 0; i < _modelArr1.count; i++) {
        TouchView * touchView = [[TouchView alloc] initWithFrame:CGRectMake(KTableStartPointX + KButtonWidth * (i%numPerLine)+HorizontalSpacing*(i%numPerLine), KTableStartPointY + KButtonHeight * (i/numPerLine)+LongitudinalSpacing*(i/numPerLine), KButtonWidth, KButtonHeight)];
        [touchView setBackgroundColor:[UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0]];
        
        [_viewArr1 addObject:touchView];
        [touchView release];
        touchView->_array = _viewArr1;
        if ([self isDeadItem:i]) {//设置固定按钮的颜色
            [touchView.label setTextColor:[UIColor colorWithRed:187/255.0 green:1/255.0 blue:1/255.0 alpha:1.0]];
        }
        else{
            
            [touchView.label setTextColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]];
        }
        touchView.label.text = [[_modelArr1 objectAtIndex:i] title];
        touchView.label.font = [UIFont systemFontOfSize:ItemFontSize];
        touchView.label.numberOfLines = 0;
        [touchView.label setTextAlignment:NSTextAlignmentCenter];
        [touchView setMoreChannelsLabel:_titleLabel2];
        touchView->_viewArr11 = _viewArr1;
        touchView->_viewArr22 = _viewArr2;
        [touchView setTouchViewModel:[_modelArr1 objectAtIndex:i]];
        
        [self.view addSubview:touchView];
    }
    
    for (int i = 0; i < modelArr2.count; i++) {
        TouchView * touchView = [[TouchView alloc] initWithFrame:CGRectMake(KTableStartPointX + KButtonWidth * (i%numPerLine)+HorizontalSpacing*(i%numPerLine), KTableStartPointY + KDeltaHeight + [self array2StartY] * KButtonHeight + KButtonHeight * (i/numPerLine)+LongitudinalSpacing*(i/numPerLine)  , KButtonWidth, KButtonHeight)];
        
        [touchView setBackgroundColor:[UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0]];
        
        [_viewArr2 addObject:touchView];
        touchView->_array = _viewArr2;
        
        touchView.label.text = [[modelArr2 objectAtIndex:i] title];
        touchView.label.font = [UIFont systemFontOfSize:ItemFontSize];
        touchView.label.numberOfLines = 0;
        [touchView.label setTextColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]];
        [touchView.label setTextAlignment:NSTextAlignmentCenter];
        [touchView setMoreChannelsLabel:_titleLabel2];
        touchView->_viewArr11 = _viewArr1;
        touchView->_viewArr22 = _viewArr2;
        [touchView setTouchViewModel:[modelArr2 objectAtIndex:i]];
        
        [self.view addSubview:touchView];
        
        [touchView release];
        
    }
    
    

    
      //尾部图片
//    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.backButton setFrame:CGRectMake(self.view.bounds.size.width - 56, self.view.bounds.size.height - 44, 56, 44)];
//    [self.backButton setImage:[UIImage imageNamed:@"order_back.png"] forState:UIControlStateNormal];
//    [self.backButton setImage:[UIImage imageNamed:@"order_back_select.png"] forState:UIControlStateNormal];
//    [self.view addSubview:self.backButton];
}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
    if (self.didPopVC!=nil) {
        self.didPopVC();
    }
}

- (void)dealloc{
    [_backButton release];
    [_titleArr release];
    [_urlStringArr release];
    [_titleLabel2 release];
    [_titleLabel release];
    [_viewArr1 release];
    [_viewArr2 release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO];
    
    
}

- (unsigned long )array2StartY{
    unsigned long y = 0;

    y = _modelArr1.count/numPerLine + 2;
    if (_modelArr1.count%numPerLine == 0) {
        y -= 1;
    }
    return y;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)isDeadItem:(NSInteger)i //添加固定选项
{
    TouchViewModel *touchViewModel = _modelArr1[i];
    NSLog(@"%@",touchViewModel.title);
    NSArray *deadItemList = [NSArray arrayWithObjects:DeadItemList, nil];
    
    for (NSString *itemId in deadItemList) {
        if ([touchViewModel.urlString isEqualToString:itemId]) {
            return YES;
        }
        
    }
    return NO;
}

@end
